/**
 * API服务层
 * 封装所有接口调用
 * @author software
 */

import request, {
	http
} from './request.js'

// 用户相关API
export const userApi = {
	// 用户登录
	login: (data) => request.post('/api/auth/login', data),
	// 用户登录微信手机号登录
	// wxlogin: (code) => request.post('/api/auth/wxLogin', code),
	wxlogin: (code1, code2) => request.post(
		`/api/auth/wxLogin?code1=${encodeURIComponent(code1)}&code2=${encodeURIComponent(code2)}`),
	// 用户注册
	register: (data) => request.post('/api/auth/register', data),
	// 修改密码
	changePssword: (data) => request.put('/api/auth/password', data),
	// 绑定手机号
	bindPhone: (code) => request.put('/api/auth/bindPhone', code),
	// 解绑手机号
	unbindPhone: () => request.put('/api/auth/unbindPhone'),
	// 绑定微信openid
	bindWechat: (data) => request.put('/api/auth/bindWechat', data),
	// 修改昵称
	changeNickname: (nickname) => request.put('/api/auth/nickname', nickname),

	// 获取用户信息
	getUserInfo: () => request.get('/api/user/info'),
	// 更新用户信息
	updateUserInfo: (data) => request.put('/api/user/info', data),
	// 上传头像
	uploadAvatar: (userId, file) => request.upload(`/api/auth/avatar/${userId}`, file),
	// 退出登录
	logout: () => request.post('/api/user/logout')
}

// 软件相关API
export const appApi = {
	// 获取软件列表
	getList: (data) => request.get('/api/software/list', data),
	// 获取软件详情
	getDetail: (appId) => request.get(`/api/software/detail/${appId}`),
	// 搜索软件
	getListBytitle: (data) => request.get(`/api/software/search?title=${data}`),
	// 发布软件
	publish: (data) => request.post('/api/software/add', data),
	// 搜索分类软件
	getListByCategory: (data) => request.get('/api/software/category',data),
	
	getLinks: (softwareId) => request.get(`/api/link/software/${softwareId}`),


	// 更新汽车信息
	update: (carId, data) => request.put(`/api/car/${carId}`, data),
	// 删除
	delete: (carId) => request.delete(`/api/car/${carId}`),
	// 下架汽车
	offline: (carId) => request.put(`/api/car/offline/${carId}`),

	// 下架汽车
	rePublish: (carId) => request.put(`/api/car/publish/${carId}`),

	// 搜索汽车 编号搜索
	searchCarNumber: (params) => request.get('/api/car/search/carNumber',
		params
	),


	// 搜索汽车 品牌
	getListByBrand: (data) => request.get('/api/car/search/brand', data),

	// 搜索汽车 关键词
	searchTitle: (params) => request.get('/api/car/search/title',
		params),


	// 根据用户ID获取汽车列表
	getCarsById: (userId) => request.get(`/api/car/user/${userId}`),

	// 收藏汽车
	favorite: (data) => request.post(`/api/favorite`, data),

	// 取消收藏
	unfavorite: (data) => request.delete(`/api/favorite?userId=${data.userId}&carId=${data.carId}`),
	// unfavorite: (params) => {
	// 	console.log('DELETE /api/favorite  params =', params.userId); // 这里打印
	// 	// return request.delete('/api/favorite', {
	// 	// 	params
	// 	// });
	// },
	// 获取收藏状态
	getFavoriteStatus: (params) => request.get('/api/favorite/status',
		params
	),

	// 获取收藏列表
	getFavorites: (userId) => request.get(`/api/favorite/user/${userId}`)
}



// 通用API
export const commonApi = {
	// 上传文件
	uploadFile: (filePath, config = {}) => request.upload('/api/upload/file', filePath, config),

	// 上传图片
	uploadImage: (filePath) => request.upload('/api/upload/image', filePath),

	// 文字检测
	textCheck: (data) => request.post('/api/check/wx/text', data),

	// 媒体检测（图片，音频）
	mediaCheck: (data) => request.post('/api/check/wx/media', data),
}

// 导出所有API
export default {
	user: userApi,
	app: appApi,
	common: commonApi
}

// 使用示例：
/*
import api from '@/utils/api.js'

// 登录
const login = async () => {
  try {
    const result = await api.user.login({
      username: 'test',
      password: '123456'
    })
    console.log('登录成功:', result)
  } catch (error) {
    console.error('登录失败:', error)
  }
}

// 获取汽车列表
const getCarList = async () => {
  try {
    const result = await api.car.getList({
      page: 1,
      size: 10,
      brand: '宝马',
      price: '10-20'
    })
    console.log('汽车列表:', result)
  } catch (error) {
    console.error('获取失败:', error)
  }
}

// 上传图片
const uploadImage = async (filePath) => {
  try {
    const result = await api.common.uploadImage(filePath)
    console.log('上传成功:', result)
  } catch (error) {
    console.error('上传失败:', error)
  }
}
*/